<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CAIXUDONG
 * Date: 11-6-20
 * Time: 下午4:17
 */
 
class RoleModel extends Model{

    public function get($role_id){

        $sql = "select * from roles where role_id = :role_id";

        return $this->db()->getRow($sql,array('role_id'=>$role_id));
    }

    public function getRoleList($curr_page,$per_page){

        $ret = $this->pager($curr_page, $per_page)
                ->ct('select count(*) from roles')
                ->rs('select * from roles order by role_id desc');
        return $ret;
    }

    public function getRoles(){
        $sql = "select * from roles order by role_id desc";

        return $this->db()->getAll($sql);
    }

    public function addRole($role_name){

        $sql = "insert into roles(role_name) values(:role_name)";

        $bindarr = array(
            'role_name'=>$role_name,
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }
    
    public function delRole($role_id){

        $sql = "delete from roles where role_id = :role_id";

        $bindarr = array(
            'role_id'=>$role_id,
        );
        $rs = $this->db()->execute($sql,$bindarr);

        return $rs->rowCount();
    }

    public function updateRole($role_id,$role_name){

        $bindarr = array(
            'role_name'=>$role_name,
            'role_id'=>$role_id,
        );
        $sql = "update roles set role_name = :role_name where role_id = :role_id";
        $rs = $this->db()->execute($sql,$bindarr);
        return $rs->rowCount();
    }

    public function getCateRoles($cate_id){

        $sql = "select r.* from roles r left join cate_has_roles cr on r.role_id = cr.role_id where cr.cate_id = :cate_id";

        return $this->db()->getAll($sql,array('cate_id'=>$cate_id));
    }

    public function getRoleCates($role_id){

        $sql = "select * from cate_has_roles where role_id = :role_id";

        return $this->db()->getAll($sql,array('role_id'=>$role_id));
    }
}